package com.example.qcweb;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.modal.WorkingProcedure;
import com.example.qc.R;

public class Fragment_StepContent extends Fragment {

    public WorkingProcedure wpProcedure;
    private TextView tView, tView2, tvstepContent, tvproperty, tvoperator,
            tvrechecker, tvchecker, tvprocess, tvarmy, tvdesigner, tvother;
    private String View, iew2, stepContent, property, operator, rechecker,
            stepchecker, process, army, designer, other;
    private String SystemId, activityname = "Fragment_StepContent";
    private String controlname = null;
    private Intent myAssiIntent;
    private String username;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        wpProcedure = (WorkingProcedure) getArguments().getSerializable("key");
        SystemId = wpProcedure.getSystemId().toString();
        // ctlsign = new String[7];
    }

    static Fragment_StepContent newInstance(WorkingProcedure wp) {
        Fragment_StepContent f = new Fragment_StepContent();
        Bundle args = new Bundle();
        args.putSerializable("key", wp);
        f.setArguments(args);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        // return super.onCreateView(inflater, container, savedInstanceState);
        // TextView tView=(TextView)getActivity().findViewById(R.id.TE)
        // return inflater.inflate(R.layout.fragment_stepcontent, container,
        // false);
        View v = inflater.inflate(R.layout.fragment_stepcontent, container,
                false);
        tView = (TextView) v.findViewById(R.id.Cardtext);
        tView2 = (TextView) v.findViewById(R.id.StepTEXT);
        tvstepContent = (TextView) v.findViewById(R.id.StepContent);
        tView.setText(Gongyi_Activity.producestrString);
        tView2.setText("工步" + wpProcedure.getStep() + ":"
                + wpProcedure.getContent());
        tvproperty = (TextView) v.findViewById(R.id.tvproperty);
        tvoperator = (TextView) v.findViewById(R.id.tvoperator);
        tvrechecker = (TextView) v.findViewById(R.id.tvrechecker);
        tvchecker = (TextView) v.findViewById(R.id.tvstepchecker);
        tvprocess = (TextView) v.findViewById(R.id.tvprocess);
        tvarmy = (TextView) v.findViewById(R.id.tvarmy);
        tvdesigner = (TextView) v.findViewById(R.id.tvdesigner);
        tvother = (TextView) v.findViewById(R.id.tvother);
        tvproperty.setText(wpProcedure.getCharacter());
        tvoperator.setText(wpProcedure.getOperator());
        tvrechecker.setText(wpProcedure.getInterChecker());
        tvchecker.setText(wpProcedure.getChecker());
        tvprocess.setText(wpProcedure.getTechnic());
        tvarmy.setText(wpProcedure.getMilitaryDelegate());
        tvdesigner.setText(wpProcedure.getDesigner());
        tvother.setText(wpProcedure.getOtherCooperator());
        tvstepContent.setText(wpProcedure.getContent());
        setOnclicklistener();
        return v;

    }

    private void setOnclicklistener() {
        // TODO Auto-generated method stub
        tvoperator.setOnClickListener(clickHandler);
        tvrechecker.setOnClickListener(clickHandler);
        tvchecker.setOnClickListener(clickHandler);
        tvprocess.setOnClickListener(clickHandler);

    }

    View.OnClickListener clickHandler = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String operator = tvoperator.getText().toString();
            String rechecker = tvrechecker.getText().toString();
            String stepchecker = tvchecker.getText().toString();
            String process = tvprocess.getText().toString();
            String army = tvarmy.getText().toString();

            switch (v.getId()) {
                // Operator进行签字
                case R.id.tvoperator:
                    if (operator.equals(null)) {

                        controlname = "operator";
                        TransferID();
                        StartAsignActivity();
                    } else {
                        controlname = "operator";
                        TransferID();
                        StartAsignActivity();

                       /* new AlertDialog.Builder(
                                Fragment_StepContent.this.getActivity())
                                .setTitle("错误提示").setMessage("请检验员刷卡")
                                .setPositiveButton("确定", null).show();*/
                        // StartAsignActivity(SystemId);
                    }
                    break;
                // 互检者进行签字
                case R.id.tvrechecker:
                    if (operator.equals(null) & rechecker.equals(null)) {

                        controlname = "rechecker";
                        TransferID();
                        StartAsignActivity();

                    } else {
                        new AlertDialog.Builder(
                                Fragment_StepContent.this.getActivity())
                                .setTitle("错误提示").setMessage("请检验员刷卡")
                                .setPositiveButton("确定", null).show();

                    }
                    break;
                // 检验签字
                case R.id.tvstepchecker:
                    if (stepchecker.equals(null)) {
                        controlname = "checker";
                        TransferID();
                        StartAsignActivity();
                    } else {

                        new AlertDialog.Builder(
                                Fragment_StepContent.this.getActivity())
                                .setTitle("警告提示").setMessage("请检验员刷卡")
                                .setPositiveButton("确定", null).show();
                        Log.i("--------------------------->tag",
                                "qingjianyanshuaka");
                    }

                    break;

                // 工艺签字
                case R.id.tvprocess:
                    if (process.equals(null)) {
                        controlname = "process";
                        TransferID();
                        StartAsignActivity();
                    } else {

                        Toast.makeText(Fragment_StepContent.this.getActivity(),
                                "请检验员刷卡", Toast.LENGTH_LONG).show();

                    }
                    break;
            }

        }

        private void TransferID() {
            // TODO Auto-generated method stub
            myAssiIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("systemid", SystemId);
            bundle.putString("activityname", activityname);
            bundle.putString("controlname", controlname);
            myAssiIntent.putExtras(bundle);
            Log.i("tag", controlname);
        }
    };

    private void StartAsignActivity() {
        /* 把bundle对象assign给Intent */
        // Intent asignIntent = new Intent();
        // SystemId=
        myAssiIntent.setClass(Fragment_StepContent.this.getActivity(),
                signactivity.class);
        startActivity(myAssiIntent);

    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

}
